//
//  KKQuestionVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKQuestionVC.h"
#import "KKQuestionCell.h"
#import "KKComHttp.h"
#import "KKQuestionSortDialog.h"
#import "KKPublishXuanShangVC.h"
#import "KKXuanShangDetailVC.h"
#import "KKWebViewVC.h"
#import "KKQuestionListM.h"
#import "KKMyHttp.h"

@interface KKQuestionVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (copy,nonatomic) NSString *type;
@property (copy,nonatomic) NSString *order;
@property (strong,nonatomic) NSArray *questionDatas;
@property (weak, nonatomic) IBOutlet UIButton *allBtn;
@property (weak, nonatomic) IBOutlet UIButton *xuanShangBtn;
@property (weak, nonatomic) IBOutlet UIButton *questionBtn;
@property (weak, nonatomic) IBOutlet UIView *sortView;
@property (weak, nonatomic) IBOutlet UILabel *sumPrice;

@end

@implementation KKQuestionVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getQuestionList)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKQuestionCell" bundle:nil] forCellReuseIdentifier:@"KKQuestionCell"];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sort)];
    [self.sortView addGestureRecognizer:tap];
    self.allBtn.selected=YES;
    
    //（全部:all,悬赏:xs、提问:tw）、排序（热度高到底:browser_up、热度低到高:browser_down、奖金:money_up、奖金倒叙:money_down）
    self.type=@"all";
    self.order=@"";
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKQuestionCell *question=(KKQuestionCell *)cell;
    [question initData:self.questionDatas[indexPath.row]];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.questionDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKQuestionCell"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKQuestionListM *question= self.questionDatas[indexPath.row];
    if(question.users!=nil&&question.users.count>0){
        return 205;
    }else{
        return 135;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKXuanShangDetailVC *detail=[[KKXuanShangDetailVC alloc] init];
    KKQuestionListM *question=self.questionDatas[indexPath.row];
    detail.questionId=question.questionid;
    detail.xuanShangMoney=question.money;
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark 按钮--提问
- (IBAction)publishClick:(id)sender {
    [KKMyHttp userCheck:^(id data) {
        KKPublishXuanShangVC *xuanShang=[[KKPublishXuanShangVC alloc] init];
        xuanShang.publishSuccessBlock=^(){
            [self.tableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:xuanShang animated:YES];
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark 按钮--官网消息与广告
- (IBAction)msgAndAdClick:(id)sender {
//    KKWebViewVC *webView=[[KKWebViewVC alloc] init];
//    webView.navTitle=@"官网消息与广告";
//    webView.url=@"http://www.baidu.com";
//    [self.navigationController pushViewController:webView animated:YES];
}

- (IBAction)allClick:(id)sender {
    self.allBtn.selected=YES;
    self.xuanShangBtn.selected=NO;
    self.questionBtn.selected=NO;
    self.type=@"all";
    [self.tableView.mj_header beginRefreshing];
}
- (IBAction)xuanShangClick:(id)sender {
    self.allBtn.selected=NO;
    self.xuanShangBtn.selected=YES;
    self.questionBtn.selected=NO;
    self.type=@"xs";
    [self.tableView.mj_header beginRefreshing];
}
- (IBAction)questionClick:(id)sender {
    self.allBtn.selected=NO;
    self.xuanShangBtn.selected=NO;
    self.questionBtn.selected=YES;
    self.type=@"tw";
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 按钮--筛选
-(void)sort{
    KKQuestionSortDialog *questionDialog=[[[NSBundle mainBundle] loadNibNamed:@"KKQuestionSortDialog" owner:self options:nil] lastObject];
    questionDialog.itemClickBlock=^(NSString *code){
        self.order=code;
        [self.tableView.mj_header beginRefreshing];
    };
    [questionDialog show];
}


#pragma mark 接口--提问回答列表
-(void)getQuestionList{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"type":self.type,@"order":self.order};
    [KKComHttp getQuestionList:params success:^(id data) {
        NSString *sumPrice=[data objectForKey:@"sumprice"];
        float temp=[sumPrice floatValue]/10000;
        self.sumPrice.text=[NSString stringWithFormat:@"%.1f万元",temp];
        NSArray *list=[data objectForKey:@"list"];
        NSArray *datas=[KKQuestionListM mj_objectArrayWithKeyValuesArray:list];
        self.questionDatas=datas;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
@end
